Learning Flex Basics (Part 4): Working with Data Models
Table of Contents
- Introduction
- Create the Application Object and Application Layout
- Add the Form Controls and the DataGrid Controls
- Add the New Employee Model, Employee, and Location Models
- Instantiate the DataController Class, and Create the Employee and DataController Classes
- Add the DataController Methods
- Code Listing for data.mxml
- Code Listings for DataController.as and Employee.as
- Code Listings for locations.xml and employee.xml
- Where to Go From Here
Create the Application Object
To begin any Flex application, start with an XML declaration and an Application tag.
The Application tag will include a namespace declaration for the MX class
library: xmlns:mx="http://www.macromedia.com/2003/mxml". The mx prefix is
used with all tags for this library.
- Create a new file and save it as data.mxml in the flex_tutorials directory.
-
At the beginning of the file, insert the XML declaration:
<?xml version="1.0">
-
After the XML declaration, add an
Applicationtag with the MXML namespace:<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml"> </mx:Application>
Create the Application Layout
You create the application layout using containers covered in Learning Flex Basics (Part 3): Working with Containers. Here, you use HBox, VBox, Panel, and Form containers to create a layout consisting of a main panel that contains a form, and two smaller panels placed to the right of the main panel.
-
Inside the Application block, add an HBox container with no properties:
<mx:HBox>
</mx:HBox> -
Inside the HBox container, add a Panel container with the following properties:
<mx:Panel id="empPanel" title="Employee Data" height="{engPanel.height+supPanel.height}"> </mx:Panel>Note: Bind this panel height to the the sum of the heights of two panels you will create beside this one—this maintains the proportions of the application without hard coding the heights. You will bind the widths of the two panels on the right to this one in the same way later.
-
Inside the Panel, add a Form container with no properties:
<mx:Form> </mx:Form>
-
After the Form container, but still inside the Panel container, add a ControlBar container with no properties:
<mx:ControlBar> </mx:ControlBar>
-
After the Panel container, add a VBox container with no properties:
<mx:VBox> </mx:VBox>
-
Inside the VBox container, add another Panel with the properties as shown:
<mx:Panel id="engPanel" title="Engineering Group" width="{empPanel.width}"> </mx:Panel> -
Still inside the VBox container, but after the Engineering Group Panel, add another Panel container with the properties as shown:
<mx:Panel id="supPanel" title="Support Group" width="{empPanel.width}"> </mx:Panel>